<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/7/1
 * Time: 14:18
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Book extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.book_name|a.book_author'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['a.book_status'] = $province;
            }
            if(isset($topic_type)&&$topic_type!="")
            {
                $map['a.book_itemize_id'] = $topic_type;
            }
//            if(isset($start) && $start != '' && isset($end) && $end != ''){
//                $map['topic_fb_time'] = ['between', [$start, $end]];
//            }
//            if(isset($start) && $start != '' && isset($end) && $end == ''){
//                $map['topic_fb_time'] = ['> time', $start];
//            }
//            if(isset($start) && $start == '' && isset($end) && $end != ''){
//                $map['topic_fb_time'] = ['< time', $end];
//            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('book')->alias('a')
                ->join('up_book_itemize ubi','ubi.book_itemize_id = a.book_itemize_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('book')->alias('a')
                ->where($map)
                ->join('up_book_itemize ubi','ubi.book_itemize_id = a.book_itemize_id','left')
                ->page($NowPage,$limits)
                ->field('a.*,ubi.book_itemize_name')
                ->order('a.book_add_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?book_id')){
            session('book_id',null);
        }
        $book_itemize = Db::table('up_book_itemize')->select();
        $this->assign('book_itemize',$book_itemize);
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('book')->where('book_id',$task_id)->setField('book_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('book')->where('book_id',$task_id)->setField('book_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
    
    /**
     * 添加书籍
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(Request::instance()->isPost()){
            $book_name = input('param.book_name');
            $book_author = input('param.book_author');
            $book_itemize_id = input('param.book_itemize_id');
            $book_price = input('param.book_price');
            $banner_thumb = input('param.banner_thumb');
            $book_brie= input('param.book_brie');
            $book_link = input('book_link');
            
            $data['book_name'] = $book_name;
            $data['book_author'] = $book_author;
            $data['book_itemize_id'] = $book_itemize_id;
            $data['book_price'] = $book_price;
            $data['book_img'] = $banner_thumb;
            $data['book_brie'] = $book_brie;
            $data['book_link'] = $book_link;
            $data['book_add_time'] = date('Y-m-d H:i:s');

            $res = Db::table('up_book')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }

        $info = Db::table('up_book_itemize')
            ->field('book_itemize_id, book_itemize_name')
            ->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
    
    /**
     * 更新书籍
     * @return mixed|\think\response\Json
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $book_name = input('param.book_name');
            $book_author = input('param.book_author');
            $book_itemize_id = input('param.book_itemize_id');
            $book_price = input('param.book_price');
            $banner_thumb = input('param.banner_thumb');
            $book_brie= input('param.book_brie');
            $book_link= input('param.book_link');
            
            $data['book_name'] = $book_name;
            $data['book_author'] = $book_author;
            $data['book_itemize_id'] = $book_itemize_id;
            $data['book_price'] = $book_price;
            $data['book_link'] = $book_link;
            $data['book_updatetime'] = date('Y-m-d H:i:s');
            if(!empty($banner_thumb)){
                $data['book_img'] = $banner_thumb;
            }
            $data['book_brie'] = $book_brie;

            $res = Db::table('up_book')->where('book_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.book_id');
        $res = Db::table('up_book')->where('book_id',$id)->find();
        $info= Db::table('up_book_itemize')->select();
        $this->assign('res',$res);
        $this->assign('id',$id);
        $this->assign('info',$info);
        return $this->fetch();
    }
    
    public function detail(){
        $id = input('param.book_id');
        $res = Db::table('up_book')->alias('a')
            ->join('up_book_itemize ubi','ubi.book_itemize_id = a.book_itemize_id','left')
            ->field('a.*,ubi.book_itemize_name')
            ->where('a.book_id',$id)->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
//        $num = Db::table('up_book_children')->where('book_id',$task_id)->count();
//        if($num>0){
//             Db::table('up_book_children')->where('book_id',$task_id)->delete();
//        }
        $res = Db::table('up_book')->where('book_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    
    /**
     * 章节列表
     * @return mixed|\think\response\Json
     */
    public function entry(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.book_children_name'] = ['like',"%" . $keyword . "%"];
            }

            $map['book_id'] =session('book_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('book_children')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('book_children')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('book_children_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?book_id')){
            session('book_id',input('book_id'));
        }
        return $this->fetch();
    }

    public function add1(){
        if(Request::instance()->isPost()){
            $book_children_name = input('param.book_children_name');
            $banner_thumb = input('param.banner_thumb');
            $book_children_detail = input('param.book_children_detail');
            $data['book_children_name'] = $book_children_name;
            $data['book_children_url'] = $banner_thumb;
            $data['book_children_detail'] = $book_children_detail;
            $data['book_children_time'] = date('Y-m-d H:i:s');
            $data['book_id'] = session('book_id');
            $res = Db::table('up_book_children')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        return $this->fetch();
    }
    public function update1(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $book_children_name = input('param.book_children_name');
            $banner_thumb = input('param.banner_thumb');
            $book_children_detail = input('param.book_children_detail');
            $data['book_children_detail'] = $book_children_detail;
            $data['book_children_name'] = $book_children_name;
            $data['book_children_updatetime'] = date('Y-m-d H:i:s');
            if(!empty($banner_thumb)){
                $data['book_children_url'] = $banner_thumb;
            }
            $res = Db::table('up_book_children')->where('book_children_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('param.book_children_id');
        $info = Db::table('up_book_children')->where('book_children_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('info',$info);
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del1(){
        $task_id = input('param.id');
        $res = Db::table('up_book_children')->where('book_children_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


    /***
     * @return \think\response\Json
     * 上架
     */
    public function up1(){
        $task_id = input('param.id');
        $res = Db::name('book')->where('book_id',$task_id)->setField('book_recommend',1);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 下架
     */
    public function down1(){
        $task_id = input('param.id');
        $res = Db::name('book')->where('book_id',$task_id)->setField('book_recommend',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
}